/* Copyright (c) 2008 Kean Loong Tan http://www.gimiti.com/kltan
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * Name: jSize
 * Version: 1.0 (April 29, 2008)
 * Requires: jQuery 1.2+
 */
 (function($) {
	// plugin definition
	$.fn.jSize = function(options) {
		var opts = $.extend({}, $.fn.jSize.defaults, options);
		var x, y, iniW, iniH, sliderW, sliderH, minW, maxW, minH, maxH, obj, curH;
		var allow=false;
		
		if (opts.type == "left-right") {
			//initialize
			//$(this).height($(this).parent().height()+'px');
			sliderW = $(this).width();
			minW = $(this).parent().width() - sliderW - parseInt($(opts.static).css("max-width"));
			maxW = $(this).parent().width() - sliderW - parseInt($(opts.static).css("min-width"));
			
			/*if(!$.browser.msie)
				if (opts.c1 == opts.static) {
					//$(opts.c2).css("min-width", minW+'px');
					//$(opts.c2).css("max-width", maxW+'px');
				}
				else if (opts.c2 == opts.static) {
					//$(opts.c1).css("min-width", minW+'px');
					//$(opts.c1).css("max-width", maxW+'px');
				}
			*/	
			//get initial mouse location
			$(this).mousedown(function(e){
				allow = true;
				x=e.pageX;
				iniW = $(opts.c1).width();
			});
			
			//mouseup anywhere at the screen
			$().mouseup(function(e){
				allow = false;
			});
			
			//get difference of mouse movement and apply to width
			$().mousemove(function(e){
				if (allow) {
					var left =  iniW - (x - e.pageX);
					//var right = parseInt($(opts.c2).parent().width()) - left - sliderW;
					$(opts.c1).css("width", left + "px");
					$(opts.c2).css("margin-left", $(opts.c1).width() + sliderW + "px");
					//$(opts.c2).css("width",  right + "px");
					//alert(right);
					
					$("#debug").html("mousemove: "+ (x- e.pageX) + "<br>left: "+ left + "<br>.left: "+ $(opts.c1).css("width") +  "<br>right's leftmargin: " + $(opts.c2).css("margin-left"));
				}
			});
		}
		
		else if (opts.type == "top-bottom") {
			//initialize
			//$(this).height($(this).parent().height()+'px');
			sliderH = $(this).height();
			minH = $(this).parent().height() - sliderH - parseInt($(opts.static).css("max-height"));
			maxH = $(this).parent().height() - sliderH - parseInt($(opts.static).css("min-height"));

			if (opts.c1 == opts.static) {
				curH = $(opts.c1).parent().height()-$(opts.c1).height()- sliderH;
				$(opts.c2).css("min-height", minH+'px');
				$(opts.c2).css("max-height", maxH+'px');
				$(opts.c2).height(curH + "px");
			}
			else if (opts.c2 == opts.static) {
				curH = $(opts.c2).parent().height()-$(opts.c2).height()- sliderH;
				$(opts.c1).css("min-height", minH+'px');
				$(opts.c1).css("max-height", maxH+'px');
				$(opts.c1).height(curH + "px");
			}
			
			//get initial mouse location
			$(this).mousedown(function(e){
				allow = true;
				y=e.pageY;
				iniH = $(opts.c1).height();
				obj = this;
			});
			
			//mouseup anywhere at the screen, disallow any resize
			$().mouseup(function(e){
				allow = false;
			});
			
			//get difference of mouse movement and apply to height
			$().mousemove(function(e){
				if (allow) {
					var top =  iniH - (y - e.pageY);
					var rest = $(obj).parent().height() - top - sliderH;
					
					$(opts.c1).css("height", top + "px");
					$(opts.c2).css("height", rest + "px");
					
					$("#debug").html("mousemove: "+ (y- e.pageY) + "<br>calced: "+ top + "<br>.top: "+ $(opts.c1).css("height") +  "<br>.bottom: " + $(opts.c2).height() + " <br>parent height: "+$(obj).parent().height());
				}
			});
		}


	}; //end jSize
	
	// plugin defaults
	$.fn.jSize.defaults = {
		type: "left-right"
	};//end jSizeDefaults;

})(jQuery);